/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pakazaka.controllers;

import com.pakazaka.enums.ItemPropertyType;
import com.pakazaka.enums.MoveDirectionType;
import com.pakazaka.enums.PlayerStateType;
import com.pakazaka.layer.TiledLayer;
import com.pakazaka.player.Player;
import static com.pakazaka.player.Player.EffectType.HUNGER;
import static com.pakazaka.player.Player.EffectType.THIRST;
import com.pakazaka.roguelike.ui.ItemDescWindow;
import com.pakazaka.simplecrawler.Core;
import static com.pakazaka.simplecrawler.Core.getInstance;
import com.pakazaka.testItemsSystem.Item;
import com.pakazaka.testItemsSystem.ItemPrototype;
import java.awt.event.KeyEvent;
import java.io.UnsupportedEncodingException;

/**
 * @author Riseremi
 */
public class ItemActionController {

    public static void processItemActions(KeyEvent e, ItemDescWindow window) {
        Player player = getInstance().getPlayer();
        TiledLayer layer = getInstance().getLayer();
        Core core = getInstance();

        Item item = window.getItem();
        final Item item1 = new Item(item.getAfterUse(), item.getTypeInType());
        boolean newItemExists = (item1.getName() != null);

        //consume item
        if (e.getKeyCode() == KeyEvent.VK_W) {
            if (item.isPropertyExist(ItemPropertyType.CAN_WEAR)) {
                if (e.isShiftDown()) {
                    player.getEquip().equipItem(item);
                } else {
                    player.getEquip().equipItem(item);
                }
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_C) {
            if (item.isPropertyExist(ItemPropertyType.CAN_DRINK)) {
                //tesuto nya desu
                final int length = ItemPrototype.getInstance().getEffectsById(item.getId()).length;
                Player.EffectType[] effects = ItemPrototype.getInstance().getEffectsById(item.getId());
                for (int i = 0; i < length; i++) {
                    switch (effects[i]) {
                        case THIRST:
                            player.applyEffect(Player.EffectType.THIRST, item.getPower());
                            break;
                        case DRUNKENNESS:
                            player.applyEffect(Player.EffectType.DRUNKENNESS, item.getPower());
                            break;
                    }
                }

                core.getConsole().pushMessage("You've drunk your " + item.getName().toLowerCase());
                player.replaceItem(item, item1, newItemExists);
                player.setState(PlayerStateType.NORMAL);
                core.getInventory().setPaint(false);
            }

            if (item.isPropertyExist(ItemPropertyType.CAN_EAT)) {
                //tesuto
                final int length = ItemPrototype.getInstance().getEffectsById(item.getId()).length;
                Player.EffectType[] effects = ItemPrototype.getInstance().getEffectsById(item.getId());

                for (int i = 0; i < length; i++) {
                    switch (effects[i]) {
                        case HUNGER:
                            player.applyEffect(Player.EffectType.HUNGER, item.getPower());
                            break;
                    }
                }

                core.getConsole().pushMessage("You've eat your " + item.getName().toLowerCase());
                player.replaceItem(item, item1, newItemExists);
                player.setState(PlayerStateType.NORMAL);
                core.getInventory().setPaint(false);
            }
        }

        //drop item
        if (e.getKeyCode() == KeyEvent.VK_D) {
            if (item.isPropertyExist(ItemPropertyType.CAN_DROP)) {
                core.getInventory().switchItemCoordinatesAndDropThen(item, MoveDirectionType.UNDEFINED, player.getAbsoluteBlocksX(), player.getAbsoluteBlocksY());
            } else {
                core.getConsole().pushMessage("You cannot drop this.");
            }
            player.setState(PlayerStateType.NORMAL);
        }

        try {
            core.getInventory().inventoryDescriptions(e, player.getAbsoluteBlocksX(),
                    player.getAbsoluteBlocksY(), player.getItems());
        } catch (UnsupportedEncodingException ex) {
        }
    }
}
